<?php
define('APPCONTACT','1.00');
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the license, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, inc., 59 Temple Place - Suite 320, Boston, MA  02111-1207, USA
 */

?>

<?php structure_openBlock("table appl",apptitle($appname),""); ?>

	<p>

<?

// sanitizing variables ---> requires Flatnuke >= 2.5.7
$req     = getparam("REQUEST_URI", PAR_SERVER, SAN_NULL);
$mod     = getparam("appname",         PAR_GET,    SAN_FLAT);
$name    = getparam("name",        PAR_POST,   SAN_FLAT);
$contact = getparam("contact",     PAR_POST,   SAN_FLAT);
$subject = getparam("subject",     PAR_POST,   SAN_FLAT);
$message = getparam("message",     PAR_POST,   SAN_NULL);
$capcha  = getparam("capcha",      PAR_POST,   SAN_FLAT);


#########################################
############# CONFIGURATION #############
#########################################

// include Flatnuke main configuration
//include "config.php";

// language definitions
switch($lang) {
	case "en":
        define("_FNOME","Name");
        define("_FEMAIL","E-mail");
        define("_FMESS","Messagge");
        define("_FP_FINVIA","Enter");
		define("_CN_TITLE", "Complete the form down here to contact me directly to my email address:");
		define("_CN_SUBJECT", "Subject");
		define("_CN_ANTISPAM", "Anti-Spam: insert");
		define("_CN_ADVISORY", "Warning: by using this form you accept that your IP will be registered, <b>for security reasons only</b>,
			to avoid spam activities to my email address; the information will be attached to the mail, and will not be archieved in any database.");
		define("_CN_CLEAR", "Clean");
		define("_CN_CODERROR", "Wrong anti-spam code!");
		define("_CN_SENDOK", "The message has been send correctly!");
		define("_CN_SENDKO", "Can't send the message, maybe the mail server is not configured or is not rightly installed");
		define("_CN_ADDRESSERROR", "Check your email address, not right!");
		define("_CN_WROTE", "wrote");
		define("_CN_IP", "IP address");
	break;
	default:
        define("_FNOME","Nome");
        define("_FEMAIL","E-mail");
        define("_FMESS","Messaggio");
        define("_FP_FINVIA","Invia");
		define("_CN_TITLE", "Completa il form sottostante per contattarmi direttamente al mio indirizzo email:");
		define("_CN_SUBJECT", "Soggetto");
		define("_CN_ANTISPAM", "Anti-Spam: inserisci");
		define("_CN_ADVISORY", "Avviso: utilizzando questo form accetti che venga registrato il tuo IP, <b>al solo scopo di sicurezza</b>,
			per evitare attivita' di spam verso il mio indirizzo di posta elettronica; l'informazione verra'
			allegata alla mail spedita e non sara' archiviata in nessun database.");
		define("_CN_CLEAR", "Pulisci");
		define("_CN_CODERROR", "Codice Anti-Spam errato!");
		define("_CN_SENDOK", "Messaggio inviato con successo!");
		define("_CN_SENDKO", "Impossibile inviare il messaggio, server di posta non configurato o non correttamente installato");
		define("_CN_ADDRESSERROR", "Il tuo indirizzo email presenta qualche errore!");
		define("_CN_WROTE", "ha scritto");
		define("_CN_IP", "Indirizzo IP");
}


##########################################
############# MAIN EXECUTION #############
##########################################

if($contact==""){
	// no valid contact value is passed, starting session
//	session_start();
	// generate security code for this session
//    $capcha_id = md5(microtime().mt_rand(100000, 999999));echo "ecco il codice di sicurezza c ".$capcha_id;
	$capcha_id = rand(100000, 999999);
	$security_code = $capcha_id;
	// check the availability of GD libraries, otherwise it uses plain text
	if(function_exists('imagecreate')) {
		$sec_image_code = "<img src='applications/$mod/capcha.php?security_code=$security_code' alt='antispam code' title='antispam code'>";
	} else {
		$sec_image_code = $security_code;
	}
	// print the HTML form
	echo "<p>"._CN_TITLE."</p>\n
		<div style='margin-left: 1em'>\n
		<form action='$req' method='POST'>\n
			<label for='name'>"._FNOME.":</label><br><input type='text' name='name' size='40'><br><br>\n
			<label for='contact'>"._FEMAIL.":</label><br><input type='text' name='contact' size='40'><br><br>\n
			<label for='subject'>"._CN_SUBJECT.":</label><br><input type='text' name='subject' size='40'><br><br>\n
			<label for='message'>"._FMESS.":</label><br><textarea name='message' cols='70' rows='20'></textarea><br>\n
			<label for='capcha'>"._CN_ANTISPAM."<br>\t<b>"; printf($sec_image_code); echo "</b></label><br>\n
			<input type='text' name='capcha' value='' autocomplete='OFF' max-length='6'>\n
			<p>"._CN_ADVISORY."</p>\n
			<input type='submit' value='"._FP_FINVIA."'> <input type='reset' value='"._CN_CLEAR."'>\n
		</form>\n
		</div>\n";
} else {
	// checking the value of anti-spam code inserted
	if($capcha != $security_code) {
		// anti-spam code is NOT right
		echo _CN_CODERROR."<br>";
		unset($security_code);
		// back or automatic redirect to the index after 2 seconds
		?><p><a href="<?=$req?>">Back</a></p><meta http-equiv="Refresh" content="2; URL=<?=$req?>"><?
		return;
	}
	// anti-spam code IS right: build the argument to pass to mail() function
	$message = "$name "._CN_WROTE.":\n".stripslashes($message)."\n\n"._CN_IP.": ".getparam("REMOTE_ADDR", PAR_SERVER, SAN_NULL);
	$headers = "From: $contact\r";
	// checkin the mail address
	if(check_mail_address($contact)){
		$sendmail = mail($admin_mail, $subject, $message, $headers);
		if($sendmail){
			// the mail was correctly send, kill session security code
			echo _CN_SENDOK."<br>";
		unset($security_code);
			// back or automatic redirect to the index after 2 seconds
			?><p><a href="<?=$req?>">Back</a></p><meta http-equiv="Refresh" content="2; URL=<?=$req?>"><?
		} else {
			// the server do not support sending emails, kill session security code
			echo _CN_SENDKO."<br>";
		unset($security_code);
			?><p><a href="<?=$req?>">Back</a></p><meta http-equiv="Refresh" content="2; URL=<?=$req?>"><?
		}
	} else {
		// there was an error in the mail address, kill session security code
		echo _CN_ADDRESSERROR."<br>";
		unset($security_code);
		?><p><a href="<?=$req?>">Back</a></p><meta http-equiv="Refresh" content="2; URL=<?=$req?>"><?
	}

}

##########################################
############# FUNCTIONS USED #############
##########################################

/**
 * Check the validity and the availability of a mail address
 *
 * This function checks first of all if the mail address is written correctly;
 * moreover, some mail servers let check if the domain specified into the address
 * really exists; this feature is disabled by default: to turn it on, just change
 * $check_dns variable from FALSE to TRUE.
 *
 * @author Giovanni Piller Cottrer <giovanni.piller@gmail.com>
 *
 * @param string  $addr      Mail address to verify
 * @param boolean $check_dns Check DNS or not
 */
function check_mail_address($addr, $check_dns=FALSE) {
	if(preg_match('/^(\w|\.|\-)+@\w+(\.\w+)*\.[a-zA-Z]{2,4}$/',$addr)) {
		if($check_dns) {
			$host = explode('@', $addr);
			// Check for MX record
			if( checkdnsrr($host[1], 'MX') ) return TRUE;
			// Check for A record
			if( checkdnsrr($host[1], 'A') ) return TRUE;
			// Check for CNAME record
			if( checkdnsrr($host[1], 'CNAME') ) return TRUE;
		} else {
			return TRUE;
		}
	}
	return FALSE;
}

?>

	<!-- footer -->
	<br><hr size="1" width="98%">
	Powered by <a href="http://www.nost.it" target="_blank">Contact Module</a> 0.1<br>Copyright &#169; 2007 Daniele Deplano

	</p>

<?php structure_closeBlock(); ?>


